﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using OnlineManagement.Model;
namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// Summary description for PigletService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class PigletService : System.Web.Services.WebService
    {

        [WebMethod]
        public JsonMsg UpdatePiglet(int pigletID, string pigTypeStr, int phaseFrom, int phaseTo, string pigPenIDStr, bool isCastrate, string castrateDateStr)
        {
            JsonMsg msg = new JsonMsg() { Flag = false };
            bool growPhaseChangeResult;
            try
            {
                if (((phaseTo - phaseFrom) > 1)||((phaseTo-phaseFrom)<0))
                {
                    msg.Flag = false;
                    msg.Message = SystemMsgs.HogPhaseChangedLevelError;
                    return msg;
                }
                //成长阶段发生了改变,修改生长阶段
                if ((phaseTo - phaseFrom) == 1)
                {
                    OnlineManagement.BLL.ProcedureHandle phandle = new BLL.ProcedureHandle();
                    Guid pigPen;
                    string message = string.Empty;
                    //改变到断奶期，不需要分圈,pigpen可以为空
                    if(phaseTo<=2){
                        growPhaseChangeResult = phandle.HogsGrowPhaseChanged(null, pigletID, phaseFrom, phaseTo, DateTime.Now, null, out message);
                        msg.Flag = growPhaseChangeResult;
                        msg.Message = message;
                    }
                    //保育期需要分圈
                   else if (phaseTo == 3 && Guid.TryParse(pigPenIDStr, out pigPen))
                    {
                        growPhaseChangeResult = phandle.HogsGrowPhaseChanged(null, pigletID, phaseFrom, phaseTo, DateTime.Now, pigPen, out message);
                        msg.Flag = growPhaseChangeResult;
                        msg.Message = message;
                    }
                    else {
                        msg.Flag = false;
                        msg.Message = SystemMsgs.PigPenMissed;
                        return msg;
                    }
                    //if update hog grow phase failed, piglet update termination
                    if (!growPhaseChangeResult) {
                        return msg;
                    }
                }
                //修改let部分信息
                DateTime? castrateDate = null;
                DateTime dt;
                //if isCastrate is true, the castrateDate must be date time value.
                if (isCastrate)
                {
                    if (DateTime.TryParse(castrateDateStr, out dt))
                    {
                        castrateDate = dt;
                    }
                    else
                    {
                        msg.Flag = false;
                        msg.Message = SystemMsgs.CastrateDateNotSetting;
                        return msg;
                    }
                }
                Guid pigTypeID;
                Piglets model = new Piglets();
                model.PigletID = pigletID;
                model.TypeID = null;
                if (Guid.TryParse(pigTypeStr, out pigTypeID))
                {
                    model.TypeID = pigTypeID;
                }
                model.IsCastrate = isCastrate;
                //if isCastrate is false, the castrateDate must be empty
                model.CastrateDate = isCastrate ? castrateDate : null;

                BLL.Dal.Piglets letBll = new BLL.Dal.Piglets();
                bool result = letBll.Update(model);
                msg.Message = result ? SystemMsgs.UpdateSuccessfully : SystemMsgs.UpdateFailed;
                msg.Flag = result;
            }catch(Exception ){
                msg.Flag = false;
                msg.Message = SystemMsgs.SystemError;
            }
            return msg;
        }

    }
}
